#! /bin/bash -x

# 轮次
data_path=$1
circle_number=${2:-1}
last_point_path=${3:-}
echo data_path $data_path
echo circle_number $circle_number

output_path=${data_path}".model_output"

for i in `seq 1 $circle_number`;do
    echo last_point_path ${last_point_path}
    model_path=
    if [ ! -z "$last_point_path" ];then
        model_path=${last_point_path}-merged
    fi

    # 模型预测
    # python add_data_from_function.py $model_path

    # 训练模型
    bash -ex train.sh "$model_path" "$data_path" "$output_path"

    # 获取生成的checkout
    new_checkout=$(ls $output_path | sort -r -t '-' -k 2 -k3 | head -n 1)
    last_checkpoint=$(ls $output_path/$new_checkout | grep 'checkpoint-' | grep -v 'merged' | sort -r | head -n 1)
    last_point_path=$output_path/${new_checkout}/${last_checkpoint}
done

# 合并模型
merged_model_path=$output_path/merged_llm_model
ls $(dirname $merged_model_path)
bash -ex merge_model.sh $last_point_path $merged_model_path

# 删除旧模型
cd $output_path
rm -rf $(ls | grep -v "merged_llm_model")
